package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void saveOrUpdateLocation(String latitude, String longitude, String addrStr, Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        //先查询数据库是否含有这条记录
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if(userLocation != null){
            //修改操作
            //GeoJsonPoint 参数1  经度 参数2: 纬度
            GeoJsonPoint point = new GeoJsonPoint(Double.valueOf(longitude) , Double.valueOf(latitude) );
            userLocation.setLocation(point); //地图上的点
            userLocation.setAddress(addrStr);//地址
            userLocation.setUpdated(System.currentTimeMillis());//修改时间
            userLocation.setLastUpdated(System.currentTimeMillis());//最后一次数据修改的数据
            mongoTemplate.save(userLocation);
        }else{
            //保存操作
            userLocation = new UserLocation();
            userLocation.setUserId(userId); //赋值用户的id
            //GeoJsonPoint 参数1  经度 参数2: 纬度
            GeoJsonPoint point = new GeoJsonPoint(Double.valueOf(longitude) , Double.valueOf(latitude) );
            userLocation.setLocation(point); //地图上的点
            userLocation.setAddress(addrStr);//地址
            userLocation.setCreated(System.currentTimeMillis());//创建时间
            userLocation.setUpdated(System.currentTimeMillis());//修改时间
            userLocation.setLastUpdated(System.currentTimeMillis());//最后一次数据修改的数据
            mongoTemplate.save(userLocation);
        }





    }

    //搜附近 返回附近人的id
    @Override
    public List<Long> searchNearUser(Long userId, Long distance) {
        //1.查询到当前用户的坐标
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //2.获得用户的当前坐标
        GeoJsonPoint geoJsonPoint = userLocation.getLocation();
        //3.确定半径  参数1: 半径 参数2: 单位
        System.out.println("===>>>>>>>>>>>>>>>>"+distance);
        Distance dis = new Distance(distance , Metrics.KILOMETERS); //TODO  后续这边应该除以1000

        //4.画圆
        Circle circle = new Circle(geoJsonPoint   ,dis);

        //5.查询数据
        Query query2 = new Query(
                Criteria.where("location").withinSphere(circle)
        );
        //不要直接返回list数据
        List<UserLocation> userLocationList = mongoTemplate.find(query2, UserLocation.class);

        //6.封装ids
        List<Long> ids = new ArrayList<>();
        if(CollectionUtil.isNotEmpty( userLocationList )){
            for (UserLocation location : userLocationList) {
                ids.add(location.getUserId());
            }
        }
        return ids;
    }
}
